package controller;

import dao.RepertoryDao;
import dao.ShopDao;
import dao.StockDao;
import entity.Repertory;
import entity.Shop;
import entity.Stock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pojo.jinhuo;
import pojo.stock;
import pojo.xiaoshou;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/shop")
public class shop {
   @Autowired
    shop shop;
    @Autowired
    public ShopDao shopdao;
    @Autowired
    public StockDao stockDao;
    @Autowired
    public service.StockDao stockDaos;
    @Autowired
    public RepertoryDao repertoryDao;
    @RequestMapping("/showShop")
    @ResponseBody
    public Map<String,Object> showShop(String shopName, String typeId, String firmId, int offset, int limit){
        Map<String,Object> map=new HashMap<>();
        try {
            if(shopName!=null){
                shopName = new String(shopName.getBytes("ISO-8859-1"),"utf-8");
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if(typeId==null||typeId==""){
            typeId="0";
        }
        if(firmId==null||firmId==""){
            firmId="0";
        }
        map.put("rows",shopdao.showShops(shopName,Integer.parseInt(typeId),Integer.parseInt(firmId),offset,limit));
        map.put("total",shopdao.totalShops(shopName,Integer.parseInt(typeId),Integer.parseInt(firmId)));
        return map;
    }
    @RequestMapping("/showStock")
    @ResponseBody
    public Map<String,Object> showStock(int staticStock,String offset,int limit){
        if(staticStock==2){
            stockDao.delstock();//删除进货临时表中所有的信息
            //根据库存报警提供的信息自动往进货单中录入信息
            List<Repertory> list=repertoryDao.showbaojingss();
            Stock stock=null;
            Shop shops;
            int i=0;
            for(Repertory repertory:list){
                stock=new Stock();
                shops=shopdao.chashop(repertory.getRepertoryName(),repertory.getRepertoryColor(),repertory.getType().getIptypeid(),repertory.getModel());
               if(shops==null){
                   Shop shop=new Shop();
                   shop.setShopprice(repertory.getPrice());
                   shop.setIptypeid(repertory.getIptypeid());
                   shop.setFirmId(repertory.getFirm().getFirmId());
                   shop.setShopName(repertory.getRepertoryName());
                   shop.setShopType(repertory.getModel());
                   shop.setShopcolor(repertory.getRepertoryColor());
               shopdao.addshop(shop);
               Shop shop1=new Shop();
               shop1=shopdao.showaddshop();
                   stock.setShopId(shop1.getShopid());
                   stock.setStockNum(repertory.getLack());
                   stock.setStockTotal(repertory.getLack()*shops.getShopprice());
                   stockDao.addStocks(stock);
               }else{
                   stock.setShopId(shops.getShopid());
                   stock.setStockNum(repertory.getLack());
                   stock.setStockTotal(repertory.getLack()*shops.getShopprice());
                   stockDao.addStocks(stock);
               }
        }

        }
        return shop.showStocks(staticStock,offset,limit);
    }
    /**\
     * 查询所有的临时进货记录表
     * @param staticStock
     * @param offset
     * @param limit
     * @return
     */
    public Map<String,Object> showStocks(int staticStock,String offset,int limit){
        Map<String,Object> map=new HashMap<>();
        map.put("rows",stockDao.showOneStock(staticStock,Integer.parseInt(offset),limit));
        map.put("total",stockDao.totalOneStock(staticStock));
        return map;
    }
    /**
     * 添加进货临时表信息
     */
    @RequestMapping("/addStock")
    @ResponseBody
    public Map<String,Object> addStocks(Stock stock){
        Stock stock1=stockDao.flagStockId(stock.getShopId());

        if(stock1!=null){
            System.out.println(stock1.getShopId());
            stockDao.upStockId(stock1.getStockid(),stock.getStockNum(),stock.getStockTotal());
        }else{
            stockDao.addStock(stock);
        }

        return shop.showStocks(1,"0",4);
    }
    /**
     * 当点击取消按钮使删除所有临时进货信息
     */
    @RequestMapping("/delStateStock")
    @ResponseBody
    public Map<String,Object> delStateStock(){
        stockDao.delStateStock();
        return shop.showStocks(1,"0",4);
    }
    /**
     * 保存修改状态模态框下的临时表信息
     */
    @RequestMapping("/upStateStock")
    @ResponseBody
    public Map<String,Object> upStateStock(){
        List<Stock> list=stockDao.showStatestock(1);
        List<Stock> lists=stockDao.showStatestock(2);
        boolean falg=false;
        for (Stock stock:list){
            for (Stock stocks:lists){
                if(stock.getShopId()==stocks.getShopId()){
                    stockDao.upstock(stock.getStockNum(),stock.getStockTotal(),stocks.getStockid());
                    System.out.println(stock.getStockid());
                    stockDao.delstockId(stock.getStockid());
                    falg=true;
                    break;
                }
            }
            if(falg==false){
               stockDao.upStateStock(stock.getStockid());
            }
        }
        return shop.showStocks(2,"0",12);
    }
    /**
     * 根据临时表中的编号删除信息
     */
    @RequestMapping("/delstockId")
    @ResponseBody
    public Map<String,Object> upStateStock(int stockId,int staticStock,String offset,int limit){
        stockDao.delstockId(stockId);
        return shop.showStocks(staticStock,offset,limit);
    }
    /**
     * 统计进货表临时表中的总数量和总价钱
     */
    @RequestMapping("/stockSum")
    @ResponseBody
    public xiaoshou stockSum(){
            return stockDao.stockSum() ;
    }
    /**
     * 修改商店商品价格
     */
    @RequestMapping("/upshopprice")
    @ResponseBody
    public List<Shop> upshopprice(int shopId, double price, String firmId){
         shopdao.upsprice(shopId,price);
         return shopdao.showShop(Integer.parseInt(firmId));
    }
    /**
     * 添加新商品
     */
    @RequestMapping("/newAddShop")
    @ResponseBody
    public Map<String,Object> newAddShop(stock stock){
        stockDaos.addstockDao(stock);
        return shop.showStocks(2,"0",12);
    }
    //生成采购单
    @RequestMapping("/wancheng")
    @ResponseBody
    public Map<String,Object> wancheng(jinhuo jinhuo, HttpServletRequest request){
        stockDaos.caigodan(jinhuo,request);
        stockDao.delstock();//删除进货临时表中所有的信息
        return shop.showStocks(2,"0",12);
    }
}
